package boxe.timer.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;

import boxe.timer.Mode;
import boxe.timer.Run;
import boxe.timer.RunPack;
import boxe.timer.env.Propertie;
import boxe.timer.manager.ModeManager;
import boxe.timer.manager.RunManager;

public class RunView extends JFrame implements ActionListener{
	
	private JLabel 	lblRunName, lblLatance, lblMain, lblRunDuration, 
					lblLatDuration,  lblWaitDuration, 
					lblSndStart, lblSndStop, lblEndRunDuration, lblEndRunSound, lblEndRunColor,
					lblLatSndStart, lblRunColor, lblLatColor, lblWaitColor;
					
	private JTextField txtRunName, txtLatanceStartSound, txtLatanceEndSound, 
						txtRunStartSound, txtRunStopSound,
						txtEndRunStartSnd, txtEndRunStop,
						txtWaitStartSound, txtWaitStopSound,
						txtColorLatance, txtColorRun, txtColorWait,
						txtColorEndRun;
	
	private JButton cmdSave, cmdCancel, cmdColorLat, cmdColorRun, cmdColorWait, 
					cmdColorEnd, cmdSoundRun, cmdSoundEnd, cmdSoundLat, 
					cmdSoundRunEnd;
	private Box[] boxes;
	private JPanel cmdSouth, panelBorder, gridBagLat, panLatColor, panRun, panRunColor, 
					panEndColor, panWaitColor, gridBagRun, gridBagWait;
	private JComboBox comboLatMin, comboLatSec, comboRunMin, comboRunSec, comboEndRun,
						comboWaitSec, comboWaitMin;
	private GridBagConstraints constraints, gridLatConst, gridRunConst, gridWaitConst;
	ModeManager modeManager;
	
	
	
	public RunView(ModeManager modeManager) {

		
		
		this.modeManager = modeManager;
		boxes = new Box[2];

		
		constraints = new GridBagConstraints();
		
		//General
		panelBorder = new JPanel(new BorderLayout());
		lblRunName = new JLabel("Name");
		lblSndStart = new JLabel("Start Sound");
		lblLatSndStart = new JLabel("Start Sound");
		lblSndStop = new JLabel("End Sound");
		lblRunColor = new JLabel("Color");
		lblLatColor = new JLabel("Color");
		lblWaitColor = new JLabel("Color");
		lblRunDuration = new JLabel("Duration");
		lblLatDuration = new JLabel("Duration");
		lblWaitDuration = new JLabel("Duration");
		lblEndRunColor = new JLabel("End run color");
		lblEndRunDuration = new JLabel("End run duration");
		lblEndRunSound = new JLabel("End run sound");
		boxes[1] = Box.createHorizontalBox();
		boxes[0] = Box.createVerticalBox();
		//Action commands
		JPanel panelCmd = new JPanel(new FlowLayout(FlowLayout.CENTER));
		cmdSave = new JButton("Save");
		cmdSave.addActionListener(this);
		cmdSave.setActionCommand(Propertie.SAVE);
		cmdCancel = new JButton("Cancel");
		cmdCancel.addActionListener(this);
		cmdCancel.setActionCommand(Propertie.CANCEL);
		panelCmd.add(cmdCancel);
		panelCmd.add(cmdSave);
		
		//Run Block
		txtRunName = new JTextField();
		txtRunName.setPreferredSize(new Dimension(150, 25));
		panRun = new JPanel();
		TitledBorder titleMain = BorderFactory.createTitledBorder("General");
		panRun.setBorder(titleMain);
		panRun.add(lblRunName);
		panRun.add(txtRunName);
		panelBorder.add(panRun, BorderLayout.NORTH);
		
		
		// Latance block
		panLatColor = new JPanel();
		txtLatanceStartSound = new JTextField();
		cmdSoundLat = new JButton("...");
		cmdSoundLat.addActionListener(this);
		cmdSoundLat.setActionCommand("sndLat");
		cmdColorLat = new JButton(" ");
		cmdColorLat.addActionListener(this);
		cmdColorLat.setActionCommand("colorLat");
		cmdColorLat.add(panLatColor);
		
		comboLatSec = new JComboBox(new Propertie().getTimeArray());
		comboLatSec.setMaximumSize(new Dimension(70, 25));
		comboLatMin = new JComboBox(new Propertie().getTimeArray());
		comboLatMin.setMaximumSize(new Dimension(70, 25));
		txtLatanceStartSound.setPreferredSize(new Dimension(50, 25));
		
		JPanel comboLat = new JPanel();
		comboLat.add(comboLatMin);
		comboLat.add(comboLatSec);
		JPanel panLatance = new JPanel();
		TitledBorder titleLat = BorderFactory.createTitledBorder("Latance");
		gridBagLat = new JPanel(new GridBagLayout());
		gridLatConst = new GridBagConstraints();
		gridLatConst.fill = GridBagConstraints.BOTH;
		gridLatConst.anchor= GridBagConstraints.WEST;
		gridLatConst.insets = new Insets(8,8,0,0);
		gridLatConst.gridx=0;
		gridLatConst.gridy=0;
		gridBagLat .add(lblLatDuration, gridLatConst);
		gridLatConst.gridx=1;
		gridLatConst.gridy=0;
		gridBagLat.add(comboLat, gridLatConst);		
		gridLatConst.gridx=0;
		gridLatConst.gridy=1;
		gridBagLat.add(lblLatSndStart, gridLatConst);
		gridLatConst.gridx=1;
		gridLatConst.gridy=1;
		gridBagLat.add(txtLatanceStartSound, gridLatConst);
		gridLatConst.gridx=2;
		gridLatConst.gridy=1;
		gridBagLat.add(cmdSoundLat, gridLatConst);
		gridLatConst.gridx=0;
		gridLatConst.gridy=2;
		gridBagLat.add(lblLatColor, gridLatConst);
		gridLatConst.gridx=1;
		gridLatConst.gridy=2;
		gridBagLat.add(cmdColorLat, gridLatConst);
		panLatance.setBorder(titleLat);
		panLatance.add(gridBagLat);
		boxes[0].add(panLatance);

		// Run block
		panRunColor = new JPanel();
		panEndColor = new JPanel();
		txtRunStartSound = new JTextField();
		txtRunStopSound = new JTextField();
		txtEndRunStartSnd = new JTextField();
		cmdSoundRun = new JButton("...");
		cmdSoundRun.addActionListener(this);
		cmdSoundRun.setActionCommand("sndStart");
		cmdSoundEnd = new JButton("...");
		cmdSoundEnd.addActionListener(this);
		cmdSoundEnd.setActionCommand("sndStop");
		cmdSoundRunEnd = new JButton("...");
		cmdSoundRunEnd.addActionListener(this);
		cmdSoundRunEnd.setActionCommand("sndEnd");
		cmdColorRun = new JButton(" ");
		cmdColorRun.addActionListener(this);
		cmdColorRun.setActionCommand("colorRun");
		cmdColorEnd = new JButton(" ");
		cmdColorEnd.addActionListener(this);
		cmdColorEnd.setActionCommand("colorEnd");
		cmdColorRun.add(panRunColor);
		cmdColorEnd.add(panEndColor);
		
		
		comboRunSec = new JComboBox(new Propertie().getTimeArray());
		comboRunSec.setMaximumSize(new Dimension(70, 25));
		comboRunMin = new JComboBox(new Propertie().getTimeArray());
		comboRunMin.setMaximumSize(new Dimension(70, 25));
		comboEndRun = new JComboBox(new Propertie().getTimeArray());
		
		txtRunStartSound.setPreferredSize(new Dimension(50, 25));
		txtRunStopSound.setPreferredSize(new Dimension(50, 25));
		txtEndRunStartSnd.setPreferredSize(new Dimension(50, 25));
		
		JPanel comboRun = new JPanel();
		comboRun.add(comboRunMin);
		comboRun.add(comboRunSec);
		JPanel panRun = new JPanel();
		TitledBorder titleRun = BorderFactory.createTitledBorder("Run");
		gridBagRun = new JPanel(new GridBagLayout());
		gridRunConst = new GridBagConstraints();
		gridRunConst.fill = GridBagConstraints.BOTH;
		gridRunConst.anchor= GridBagConstraints.WEST;
		gridRunConst.insets = new Insets(8,8,0,0);
		
		gridRunConst.gridx=0;
		gridRunConst.gridy=0;
		gridBagRun .add(lblRunDuration, gridRunConst);
		gridRunConst.gridx=1;
		gridRunConst.gridy=0;
		gridBagRun.add(comboRun, gridRunConst);	
		
		gridRunConst.gridx=0;
		gridRunConst.gridy=1;
		gridBagRun.add(lblSndStart, gridRunConst);
		gridRunConst.gridx=1;
		gridRunConst.gridy=1;
		gridBagRun.add(txtRunStartSound, gridRunConst);
		gridRunConst.gridx=2;
		gridRunConst.gridy=1;
		gridBagRun.add(cmdSoundRun, gridRunConst);
		
		gridRunConst.gridx=0;
		gridRunConst.gridy=2;
		gridBagRun.add(lblSndStop, gridRunConst);
		gridRunConst.gridx=1;
		gridRunConst.gridy=2;
		gridBagRun.add(txtRunStopSound, gridRunConst);
		gridRunConst.gridx=2;
		gridRunConst.gridy=2;
		gridBagRun.add(cmdSoundEnd, gridRunConst);
		
		gridRunConst.gridx=0;
		gridRunConst.gridy=3;
		gridBagRun.add(lblRunColor, gridRunConst);
		gridRunConst.gridx=1;
		gridRunConst.gridy=3;
		gridBagRun.add(cmdColorRun, gridRunConst);
		
		//EndRun
		gridRunConst.gridx=0;
		gridRunConst.gridy=4;
		gridBagRun .add(lblEndRunDuration, gridRunConst);
		gridRunConst.gridx=1;
		gridRunConst.gridy=4;
		gridBagRun.add(comboEndRun, gridRunConst);			

		gridRunConst.gridx=0;
		gridRunConst.gridy=5;
		gridBagRun.add(lblEndRunSound, gridRunConst);
		gridRunConst.gridx=1;
		gridRunConst.gridy=5;
		gridBagRun.add(txtEndRunStartSnd, gridRunConst);
		gridRunConst.gridx=2;
		gridRunConst.gridy=5;
		gridBagRun.add(cmdSoundRunEnd, gridRunConst);
		
		gridRunConst.gridx=0;
		gridRunConst.gridy=6;
		gridBagRun.add(lblEndRunColor, gridRunConst);
		gridRunConst.gridx=1;
		gridRunConst.gridy=6;
		gridBagRun.add(cmdColorEnd, gridRunConst);
		
		panRun.setBorder(titleRun);
		panRun.add(gridBagRun);
		boxes[1].add(panRun);	
		
		//wait block
		panWaitColor = new JPanel();
		cmdColorWait = new JButton(" ");
		cmdColorWait.addActionListener(this);
		cmdColorWait.setActionCommand("colorWait");
		cmdColorWait.add(panWaitColor);
		
		comboWaitSec = new JComboBox(new Propertie().getTimeArray());
		comboWaitSec.setMaximumSize(new Dimension(70, 25));
		comboWaitMin = new JComboBox(new Propertie().getTimeArray());
		comboWaitMin.setMaximumSize(new Dimension(70, 25));
		
		JPanel comboWait = new JPanel();
		comboWait.add(comboWaitMin);
		comboWait.add(comboWaitSec);
		JPanel panWait= new JPanel();
		TitledBorder titleWait= BorderFactory.createTitledBorder("Wait");
		gridBagWait = new JPanel(new GridBagLayout());
		gridWaitConst = new GridBagConstraints();
		gridWaitConst.fill = GridBagConstraints.BOTH;
		gridWaitConst.anchor= GridBagConstraints.WEST;
		gridWaitConst.insets = new Insets(8,8,0,0);
		
		gridWaitConst.gridx=0;
		gridWaitConst.gridy=0;
		gridBagWait .add(lblWaitDuration, gridWaitConst);
		gridWaitConst.gridx=1;
		gridWaitConst.gridy=0;
		gridBagWait.add(comboWait, gridWaitConst);

		gridWaitConst.gridx=0;
		gridWaitConst.gridy=1;
		gridBagWait.add(lblWaitColor, gridWaitConst);
		gridWaitConst.gridx=1;
		gridWaitConst.gridy=1;
		gridBagWait.add(cmdColorWait, gridWaitConst);
		panWait.setBorder(titleWait);
		panWait.add(gridBagWait);
		boxes[0].add(panWait);
		
		boxes[1].add(boxes[0]);
		panelBorder.add(boxes[1], BorderLayout.CENTER);
		panelBorder.add(panelCmd, BorderLayout.SOUTH);
		//panelBorder.add(cmdSouth, BorderLayout.SOUTH);

		//cmdSave.setActionCommand(Propertie.SAVE);
		//cmdSave.addActionListener(this);
		//cmdCancel.setActionCommand(Propertie.CANCEL);
		//cmdCancel.addActionListener(this);
		this.setSize(800,500);
		this.setTitle("Run view");
		this.add(panelBorder);
//		centrage
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = this.getSize();
		this.setLocation(
			(screenSize.width - frameSize.width)/2,
			(screenSize.height-frameSize.height)/2
		);
		
		//look and feel
		try {
			UIManager.setLookAndFeel(
					UIManager.getSystemLookAndFeelClassName());
			SwingUtilities.updateComponentTreeUI(this);
		}catch (Exception e) {}
		this.pack();
	
		//frame.setIconImage(new ImageIcon(ICON).getImage());
		this.setResizable(false);
		//this.getContentPane().add(panelBorder);
		//this.setDefaultCloseOperation(ABORT);
	}



	public void actionPerformed(ActionEvent e) {
		String item = String.valueOf(((AbstractButton) e.getSource())
				.getActionCommand());
		
		if(item.equals("sndLat")){
			txtLatanceStartSound.setText(getSound("./sound/"));
		}
		else if(item.equals("sndStart")){
			txtRunStartSound.setText(getSound("./sound/"));
		}
		else if(item.equals("sndStop")){
			txtRunStopSound.setText(getSound("./sound/"));
		}
		else if(item.equals("sndEnd")){
			txtEndRunStartSnd.setText(getSound("./sound/"));
		}
		else if(item.equals("colorLat")){
			JButton target = (JButton) e.getSource();
			Color c = getColor(panLatColor.getBackground()); 
			panLatColor.setBackground(c);
		}
		else if(item.equals("colorRun")){
			JButton target = (JButton) e.getSource();
			Color c = getColor(panRunColor.getBackground()); 
			panRunColor.setBackground(c);
		}
		else if(item.equals("colorEnd")){
			JButton target = (JButton) e.getSource();
			Color c = getColor(panEndColor.getBackground()); 
			panEndColor.setBackground(c);
		}
		else if(item.equals("colorWait")){
			JButton target = (JButton) e.getSource();
			Color c = getColor(panWaitColor.getBackground()); 
			panWaitColor.setBackground(c);
		}
		else if(item.equals(Propertie.SAVE)) {
			RunPack runPack;
			if(modeManager.getNewRunPack() != null) {
				runPack = new RunPack();
				Mode mode = modeManager.getCurrentMode();
				modeManager.addRun(runPack);
				modeManager.setCurrentRunPack(runPack);
			} else {
				runPack = modeManager.getCurrentRunPack();
			}
			RunManager runMgm = new RunManager();
			
			//save latance
			runPack.setName(txtRunName.getText());
			Run run = runPack.getLatance();
		    int sec = comboLatSec.getSelectedIndex();
			int min = comboLatMin.getSelectedIndex();
			long duration = runMgm.getDuration(min, sec);
			Color color = panLatColor.getBackground();
			run.setDuration(duration);
			run.setBackground(color);
			run.setSndStart(this.txtLatanceStartSound.getText());
			
			//save run
			run = runPack.getRun();
		    sec = comboRunSec.getSelectedIndex();
			min = comboRunMin.getSelectedIndex();
			duration = runMgm.getDuration(min, sec);
			color = panRunColor.getBackground();
			run.setDuration(duration);
			run.setBackground(color);
			run.setEndRunWarning(
					runMgm.getDuration(0, comboEndRun.getSelectedIndex()));
			run.setEndRunColor(panEndColor.getBackground());
			run.setSndStart(txtRunStartSound.getText());
			run.setSndStop(txtRunStopSound.getText());
			run.setSndEndRun(txtEndRunStartSnd.getText());
			
			//save wait
			run = runPack.getWait();
		    sec = comboWaitSec.getSelectedIndex();
			min = comboWaitMin.getSelectedIndex();
			duration = runMgm.getDuration(min, sec);
			color = panWaitColor.getBackground();
			run.setDuration(duration);
			run.setBackground(color);
		    modeManager.refreshListRun();	
		    modeManager.setSelectedIndex();
			this.setVisible(false);
		}
		else if(item.equals(Propertie.CANCEL)) {
			this.setVisible(false);
		}
	}
	


	/**
	 * 
	 * Display color dialog and return a color
	 * @param color
	 * @return
	 */
	public Color getColor(Color color){
		Color c = JColorChooser.showDialog(this,    // Dialog appears over this
                "Pick a Color", // Dialog title 
                color);   // Default color selection
		return c;
	}
	
	/**
	 * Display a file chooser for sound file
	 * @param filePath
	 * @return
	 */
	public String getSound(String filePath){
		JFileChooser fileChoose = new JFileChooser(filePath);
		fileChoose.setFileSelectionMode(JFileChooser.FILES_ONLY);
		
		if (fileChoose.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){	
			return fileChoose.getSelectedFile().getAbsolutePath();
		}
		else return "";
	}
	
	/**
	 * set the run values
	 * 
	 * @param pack
	 */
	public void setDefaultValue(RunPack pack) {
		Run latance = pack.getLatance();
		RunManager runMgm = new RunManager();
		Run run = pack.getRun();
		Run wait = pack.getWait();
		int secIndex;
		int minIndex;
		Color color;
		txtRunName.setText(pack.getName());
		
		//latance
		secIndex = runMgm.getComboSec(latance.getDuration());
		minIndex = runMgm.getComboMin(latance.getDuration());
		panLatColor.setBackground(latance.getBackground());
		txtLatanceStartSound.setText(latance.getSndStart());
		comboLatSec.setSelectedIndex(secIndex);
		comboLatMin.setSelectedIndex(minIndex);
		
		//Run
		secIndex = runMgm.getComboSec(run.getDuration());
		minIndex = runMgm.getComboMin(run.getDuration());
		comboRunSec.setSelectedIndex(secIndex);
		comboRunMin.setSelectedIndex(minIndex);
		
		secIndex = runMgm.getComboSec(run.getEndRunWarning());
		//minIndex = runMgm.getComboMin(run.getEndRunWarning());
		comboEndRun.setSelectedIndex(secIndex);
		
		panRunColor.setBackground(run.getBackground());
		txtRunStartSound.setText(run.getSndStart());
		txtRunStopSound.setText(run.getSndStop());
		txtEndRunStartSnd.setText(run.getSndEndRun());
		panEndColor.setBackground(run.getEndRunColor());
		
		//Wait
		secIndex = runMgm.getComboSec(wait.getDuration());
		minIndex = runMgm.getComboMin(wait.getDuration());
		panWaitColor.setBackground(wait.getBackground());
		comboWaitSec.setSelectedIndex(secIndex);
		comboWaitMin.setSelectedIndex(minIndex);
	}
}
